package com.hejian.android.a72ggametools.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.hejian.android.a72ggametools.R;
import com.hejian.android.a72ggametools.activity.LoginActivity;
import com.hejian.android.a72ggametools.activity.GameDetailActivity;
import com.hejian.android.a72ggametools.activity.SearchActivity;
import com.hejian.android.a72ggametools.adapter.MoneyAdapter;
import com.hejian.android.a72ggametools.bean.MoneyGame;
import com.hejian.android.a72ggametools.common.Constant;
import com.hejian.android.a72ggametools.net.HttpUtils;
import com.hejian.android.a72ggametools.utils.JsonParse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/1/13.
 */
public class MoneyFragment extends Fragment implements View.OnClickListener {
    private View view;
    private RelativeLayout searchBtn;
    private Button signForScoreBtn;
    private Button todayTaskBtn;
    private Button scoreShopBtn;
    private RadioGroup radioGroup;
    private ListView moneyListView;
    private int page = 1;
    private List<MoneyGame> moneyGameList = new ArrayList<MoneyGame>();
    private MoneyAdapter moneyAdapter;
    private boolean isBottem;
    private boolean isLoadingComplete;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_money, null);
        initData();
        initView();
        setListener();
        return view;
    }

    private void setListener() {
        searchBtn.setOnClickListener(this);
        signForScoreBtn.setOnClickListener(this);
        todayTaskBtn.setOnClickListener(this);
        scoreShopBtn.setOnClickListener(this);
        setItemListener();
        setListViewScrollListener();
    }

    private void setListViewScrollListener() {
        moneyListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (AbsListView.OnScrollListener.SCROLL_STATE_IDLE == scrollState && isBottem && isLoadingComplete) {
                    isLoadingComplete = false;
                    page++;
                    loadNextPage(page);
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if ((firstVisibleItem + visibleItemCount) == totalItemCount) {
                    isBottem = true;
                } else {
                    isBottem = false;
                }

            }
        });
    }

    private void setItemListener() {
        moneyListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), GameDetailActivity.class);
                MoneyGame moneyGame = moneyGameList.get(position);
                intent.putExtra("money_item", moneyGame);
                startActivity(intent);
            }
        });
    }


    private void initView() {
        searchBtn = (RelativeLayout) view.findViewById(R.id.search_btn);
        signForScoreBtn = (Button) view.findViewById(R.id.sign_for_score_btn);
        todayTaskBtn = (Button) view.findViewById(R.id.today_task_btn);
        scoreShopBtn = (Button) view.findViewById(R.id.score_shop_btn);
        moneyListView = (ListView) view.findViewById(R.id.money_listview);


    }

    private void initData() {
        loadNextPage(page);

    }

    private void loadNextPage(final int page) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                loadOnePage(page);
            }
        }).start();
    }

    private void loadOnePage(int page) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("platform", "2");
        map.put("page", page + "");
        String buffer = HttpUtils.doPostRequest(Constant.MONEY_LIST_URL, map);
        List<MoneyGame> moneyGameList = JsonParse.parseMoneyJson(buffer);
        this.moneyGameList.addAll(moneyGameList);
        Message msg = Message.obtain();
        if (page == 1) {

            msg.what = 1;
        } else {
            msg.what = 2;
        }
        msg.obj = this.moneyGameList;
        handler.sendMessage(msg);

    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                setMoneyAdapter();
                isLoadingComplete = true;
            } else {
                moneyAdapter.notifyDataSetChanged();
                isLoadingComplete = true;
            }

        }
    };

    private void setMoneyAdapter() {
        moneyAdapter = new MoneyAdapter(moneyGameList, getActivity());
        moneyListView.setAdapter(moneyAdapter);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search_btn:
                Intent searchIntent = new Intent();
                searchIntent.setClass(getActivity(), SearchActivity.class);
                startActivity(searchIntent);
                break;
            case R.id.sign_for_score_btn:
                Intent getScoreIntent = new Intent();
                getScoreIntent.setClass(getActivity(), LoginActivity.class);
                startActivity(getScoreIntent);
                break;
            case R.id.today_task_btn:
                Intent todayTaskIntent = new Intent();
                todayTaskIntent.setClass(getActivity(), LoginActivity.class);
                startActivity(todayTaskIntent);
                break;
            case R.id.score_shop_btn:
                radioGroup = (RadioGroup) getActivity().findViewById(R.id.radio_select);
                radioGroup.check(radioGroup.getChildAt(3).getId());
                ((RadioButton) radioGroup.getChildAt(3)).setTextColor(Color.BLUE);
        }
    }

}
